package com.jxs.android.sxyw;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.jxs.android.sxyw.data.HitchInfo;
import com.jxs.android.sxyw.db.HitchHolder;
import com.jxs.android.sxyw.weiget.TopBar;

/**
 * @Des 故障程度
 * @author Jiangxs
 * 
 */
public class SelectHitch extends AbstractActivity {

	private ListView orgListview = null;
	private Context context;
	private List<HitchInfo> hitchInfos = new ArrayList<HitchInfo>();
	private HitchAdpter hitchAdapter = null;
	private TopBar topBar;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.org_with_list);

		context = this;

		if (null != getIntent() && null != getIntent().getExtras()) {
			int flag = getIntent().getExtras().getInt("flag", 1);
			if (flag == 1) {
				hitchInfos.addAll(HitchHolder.getHitchInfosWithFlag("1"));
			} else if (flag == 2) {
				hitchInfos.addAll(HitchHolder.getHitchInfosWithParentID(getIntent().getExtras().getString("parent_id")));
			}
			this.topBar.setTitle(getIntent().getExtras().getString("title"));
		}

		orgListview = (ListView) findViewById(R.id.org_listview);
		hitchAdapter = new HitchAdpter(context);
		orgListview.setAdapter(hitchAdapter);
		orgListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putSerializable("hitchInfo", hitchInfos.get(position));
				intent.putExtras(bundle);
				setResult(RESULT_OK, intent);
				SelectHitch.this.finish();
			}
		});
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		this.topBar = topBar;
		topBar.hindBtn();
		topBar.setTitle("地区选择");
		initTopBarReturn(topBar);
	}

	private class HitchAdpter extends BaseAdapter {

		private LayoutInflater inflater = null;

		public HitchAdpter(Context context) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return hitchInfos.size();
		}

		@Override
		public HitchInfo getItem(int position) {
			return hitchInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView textView = (TextView) inflater.inflate(R.layout.tv_item, null);
			if (position == 0) {
				textView.setBackgroundResource(R.drawable.roundlist_head);
			}
			if (position == hitchInfos.size() - 1) {
				textView.setBackgroundResource(R.drawable.roundlist_tail);
			}
			textView.setText("    " + hitchInfos.get(position).getName().trim());
			return textView;
		}

	}

}